package com.unittec.drainage.provider.manage.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.manage.entity.SewagePlant;
import com.unittec.drainage.provider.manage.service.SewagePlantService;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.provider.utils.ValidationUtils;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.manage.request.*;
import com.unittec.drainage.sdk.manage.response.*;
import com.unittec.drainage.sdk.manage.vo.RegionVo;
import com.unittec.drainage.sdk.manage.vo.SewagePlantVo;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;
import com.unittec.drainage.sdk.root.enums.ErrorEnum;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "污水厂管理")
public class SewagePlantController {

    @Autowired
    private Log log;

    @Autowired
    private SewagePlantService sewagePlantService;

    @RequestMapping(value = "/301020", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "污水厂列表", notes = "污水厂列表")
    public Result<Res301020> sewagePlantList(@RequestBody GatewayRequest<Req301020> req) {
        log.debug(req, "sewagePlantList  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301020 res301020 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<SewagePlantVo> list = sewagePlantService.sewagePlantList(req.getContent());
            res301020 = new Res301020();
            res301020.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "sewagePlantList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res301020);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301021", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加污水厂", notes = "添加污水厂")
    public Result<Res301021> addSewagePlant(@RequestBody GatewayRequest<Req301021> req) {
        log.debug(req, "addSewagePlant {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301021 res301021 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            sewagePlantService.addSewagePlant(req.getContent());
            res301021 = new Res301021();
            res301021.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301021", JSON.toJSONString(req), "添加污水厂", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addSewagePlant {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301021", JSON.toJSONString(req), "添加污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301021);
        } else {
            log.addOperateLog("301021", JSON.toJSONString(req), "添加污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301022", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改污水厂", notes = "修改污水厂")
    public Result<Res301022> updateSewagePlant(@RequestBody GatewayRequest<Req301022> req) {
        log.debug(req, "addSewagePlant {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301022 res301022 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            sewagePlantService.updateSewagePlant(req.getContent());
            res301022 = new Res301022();
            res301022.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301022", JSON.toJSONString(req), "修改污水厂", log.buildErrmsg(e),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addSewagePlant {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301022", JSON.toJSONString(req), "修改污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301022);
        } else {
            log.addOperateLog("301022", JSON.toJSONString(req), "修改污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301023", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除污水厂", notes = "删除污水厂")
    public Result<Res301023> deleteSewagePlant(@RequestBody GatewayRequest<Req301023> req) {
        log.debug(req, "deleteSewagePlant {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301023 res301023 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            sewagePlantService.removeById(req.getContent().getId());
            res301023 = new Res301023();
            res301023.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301023", JSON.toJSONString(req), "删除污水厂", log.buildErrmsg(e),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "deleteSewagePlant {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301023", JSON.toJSONString(req), "删除污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301023);
        } else {
            log.addOperateLog("301023", JSON.toJSONString(req), "删除污水厂", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301024", method = RequestMethod.POST)
    @ApiOperation(value = "污水厂详情", notes = "污水厂详情")
    @ResponseBody
    public Result<Res301024> getSewagePlantDetails(@RequestBody GatewayRequest<Req301024> req) {
        log.debug(req, "getSewagePlantDetails {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301024 res301024 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SewagePlantVo sewagePlantVo = sewagePlantService.getSewagePlantDetails(req.getContent().getId());
            res301024 = new Res301024();
            res301024.setSewagePlantVo(sewagePlantVo);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getSewagePlantDetails {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res301024);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }


}
